package com.confinderence;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.confinderence.WebServiceTask.IResponseHandler;
import com.confinderence.model.Conference;
import com.confinderence.model.Lecturer;
import com.confinderence.model.User;
import com.confinderence.utils.gson.ArrayAdapterFactory;
import com.confinderence.utils.gson.CustomDateTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class LoginActivity extends Activity implements IResponseHandler {

	private static final String URL = "http://" + Utils.IP
			+ "/ConferenceManager/rest/login/mobileLogin";
	private EditText mUsername;
	private EditText mPassword;
	private Button mLoginButton;
	private User mUser;
	private WebServiceTask mWebTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		init();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.logged_in_menu, menu);
		Utils.initMenu(menu, this);
		return true;
	}
	private void init() {
		mUsername = (EditText) findViewById(R.id.usernameEditView);
		mPassword = (EditText) findViewById(R.id.passwordEditView);
		mLoginButton = (Button) findViewById(R.id.loginBtn);
		mLoginButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String name;
				String pass;
				name = mUsername.getText().toString();
				pass = mPassword.getText().toString();
				if (name != null && name.length() != 0 && pass != null && pass.length() != 0) {
					verifyUsername(name, pass);
				}
			}
		});
	}

	protected void verifyUsername(String name, String pass) {
		mWebTask = new WebServiceTask(WebServiceTask.POST_TASK_WITH_JSON, this,
				getString(R.string.wait_msg), this);
		mUser = new User(name, pass);
		Gson gson = new Gson();
		String json = gson.toJson(mUser);
		json = "{\"user\":" + json + "}";
		mWebTask.setJson(json);
		mWebTask.execute(URL);
	}

	@Override
	public void handleResponse(String response) {
		if (response.length() != 0) {
			Gson gson = new GsonBuilder()
					.registerTypeAdapter(Date.class, new CustomDateTypeAdapter())
					.registerTypeAdapterFactory(new ArrayAdapterFactory()).setPrettyPrinting()
					.create();
			Type lecturerType = new TypeToken<Lecturer>() {
			}.getType();
			try {
				JSONObject jsonObject = new JSONObject(response);
				Lecturer lecturer = gson.fromJson(jsonObject.getJSONObject("lecturer").toString(),
						lecturerType);
				Utils.setLecturer(lecturer);
				Utils.signInUser(mUser, this);
				goToConferences();
			} catch (JSONException e) {
				e.printStackTrace();
			}

		} else
			Toast.makeText(this, getString(R.string.unsuccessful_login), Toast.LENGTH_LONG).show();
	}
	private void goToConferences() {
		Intent intent = new Intent(this, ConferenceActivity.class);
		startActivity(intent);
	}
}
